<?php

/**
 * Created by Atom.
 * User: qiliping
 * Date: 2017/1/18
 * Time: 11:18
 */
class Order_model extends CI_Model
{
  /**
  **  获取订单信息
  */
	public function get_order_by_id($order_id){
		$sql = "SELECT * FROM {$this->db->dbprefix}shopping_orders where order_id = {$order_id} LIMIT 1";
		return $this->db->query($sql)->row_array();
	}
  function get_commission($school_id,$conditions=array()){
    $where = " school_id = {$school_id} and order_type !=3 ";
    if($conditions['st']>0){
			$where .= " AND order_time >  ".strtotime($conditions['st']);
		}
    if($conditions['et']>0){
			$where .= " AND order_time <  ".strtotime($conditions['et']);
		}
    if($conditions['status'] == 0){
      $where .= " AND payment_status != 2 ";
    }else if($conditions['status'] == 2){
      $where .= " AND payment_status = 2 ";
    }

    $sql = "SELECT SUM(total_order_commission) as commission,SUM(total_order_amount) AS order_amount from {$this->db->dbprefix}shopping_orders WHERE $where";
    return $this->db->query($sql)->row_array();
  }
  /**
   * 获取分销的订单
   * @param integer $limit      [description]
   * @param integer $offset     [description]
   * @param [type]  $school_id  [description]
   * @param array   $conditions [description]
   */
  function get_fx_order_by_school($limit = 0, $offset = 10, $school_id,$conditions=array()){
    // print_r(strtotime($conditions['st']));exit;
    $where = " school_id = {$school_id} and order_type !=3 ";
    if($conditions['st']>0){
			$where .= " AND order_time >  ".strtotime($conditions['st']);
		}
    if($conditions['et']>0){
			$where .= " AND order_time <  ".strtotime($conditions['et']);
		}
    if($conditions['status'] == 0){
      $where .= " AND payment_status != 2 ";
    }else if($conditions['status'] == 2){
      $where .= " AND payment_status = 2 ";
    }

    $sql = "SELECT SQL_CALC_FOUND_ROWS * FROM {$this->db->dbprefix}shopping_orders WHERE $where ORDER BY order_time desc LIMIT $limit,$offset";
    $result = $this->db->query($sql)->result_array();
		$row_num =  $this->db->query('SELECT FOUND_ROWS() as row_num;')->row_array();
		$this->db->trans_complete();
		return array("total_count" => $row_num['row_num'], "result" => $result);
  }

  /**
  **  查询学校订单
  */
	public function get_order_list_by_school($limit = 0, $offset = 10, $school_id, $data){
		$where = " ord.school_id = '{$school_id}' ";
		if(!empty($data['order_type'])){
			$where .= " AND ord.order_type = '{$data['order_type']}' ";
		}
		if(!empty($data['status']) && $data['status'] != 'all'){
			$where .= " AND ord.order_status = '{$data['status']}' ";
		}
		if(!empty($data['order_num'])){
			$where .= " AND ord.order_num = '{$data['order_num']}' ";
		}
		if(!empty($data['st'])){
			$data['st'] = strtotime($data['st']);
			$where .= " AND ord.order_time > '{$data['st']}' ";
		}
		if(!empty($data['et'])){
			$data['et'] = strtotime($data['et']) + 86400;
			$where .= " AND ord.order_time < '{$data['et']}' ";
		}
		if(!empty($data['mobile'])){
			$where .= " AND u.mobile = '{$data['mobile']}' ";
		}
		if(!empty($data['uid'])){
			$where .= " AND u.id = '{$data['uid']}' ";
		}
		$this->db->trans_start();
		$sql = "SELECT SQL_CALC_FOUND_ROWS ord.*,cam.name as campus_name,u.mobile,u.nick FROM {$this->db->dbprefix}shopping_orders ord ";
		$sql .= " left join {$this->db->dbprefix}o2o_school_campus cam on cam.id = ord.school_campus_id ";
		$sql .= " left join {$this->db->dbprefix}user u on u.id=ord.buyer_user_id ";
		$sql .= " where $where ORDER BY ord.order_time DESC LIMIT $limit, $offset";
		$result = $this->db->query($sql)->result_array();
		$row_num =  $this->db->query('SELECT FOUND_ROWS() as row_num;')->row_array();
		$this->db->trans_complete();
		return array("total_count" => $row_num['row_num'], "result" => $result);
	}

	function get_order_total_amount($conditions = array()){
        $where = " order_status >= 1 ";//" order_type = $type ";
        if(!empty($conditions['order_type'])){
            $where .= " and o.order_type='{$conditions['order_type']}' ";
        }

        if(!empty($conditions['school_id'])){
            $where .= " and o.school_id='{$conditions['school_id']}' ";
        }

        if(!empty($conditions['mobile'])){
			$where .= " AND u.mobile = '{$conditions['mobile']}' ";
		}

        if(!empty($conditions['start_time'])){
            $start_time = strtotime($conditions['start_time']);
            $where .= " and o.order_time >= '{$start_time}' ";
        }

        if(!empty($conditions['end_time'])){
            $end_time = strtotime($conditions['end_time']) + 3600 * 24;
            $where .= " and o.order_time <= '{$end_time}' ";
        }
        $sql = "SELECT sum(total_order_amount) AS total_amount FROM {$this->db->dbprefix}shopping_orders AS o left join {$this->db->dbprefix}user u on u.id=o.buyer_user_id where $where";
        $result = $this->db->query($sql)->row_array();
        return $result;
    }
}
